<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>removeElement</title>
</head>

<body>

</body>
<script>
    /**
     * @param {number[]} nums
     * @param {number} val
     * @return {number}
     */
    let removeElement = function (nums, val) {
        const n = nums.length;
        let left = 0;
        for (let right = 0; right < n; right++) {
            if (nums[right] !== val) {
                nums[left] = nums[right];
                left++;
            }
        }
        return left;
    };
    let removeElement2 = function (nums, val) {
        let left = 0
        let right = nums.length
        while (left < right) {
            if (nums[left] == val) {
                nums[left] = nums[right - 1]
                right--
            } else {
                left++
            }
        }
        return left
    };
    let nums = [3, 2, 2, 3]
    const val = 3
    console.log(removeElement2(nums, val)); 
</script>

</html>